
package com.wyzz.hr.action;

import java.io.FileInputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import org.apache.axis.utils.StringUtils;
import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.wyzz.hr.base.BaseDownloadAction;
import com.wyzz.hr.bo.IAttendmonthlyBO;
import com.wyzz.hr.bo.IEmpTypeBO;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.pojo.Attendmonthly;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Emptype;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.DateUtil;
import com.wyzz.hr.util.Pager;

public class MyAttendMonthly extends BaseDownloadAction {

	public MyAttendMonthly() {
		page = new Pager();
		startYear = DateUtil.formatTodayToS("yyyy");
		startMonth = "01";
		endYear = DateUtil.formatTodayToS("yyyy");
		endMonth = DateUtil.formatTodayToS("MM");
		searchOrExport = null;
		docStream = null;
		contentDisposition = null;
		serverFileName = null;
		attendmothlyBO = (IAttendmonthlyBO) SpringBeanFactory
				.getBean("attendmonthlyBo");
		single = "yes";
		status = Integer.valueOf(2);
	}

	public String execute() throws Exception {
		Calendar cal = Calendar.getInstance();
		currYear = String.valueOf(cal.get(1));
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		if (StringUtils.isEmpty(attdEmpId)) {
			attdEmpId = getCurrentEmpNo();
			empName = getCurrentEmp().getEmpName();
		}
		Employee employee = empBo.loadEmp(attdEmpId, null);
		if (employee == null) {
			addErrorInfo("\u4E0D\u5B58\u5728\u6B64\u5458\u5DE5\uFF01");
			return "error";
		}
		initDrillDownList();
		closedYearMonthlyList = attendmothlyBO.getClosedPeriod(
				(new StringBuilder()).append(startYear).append(startMonth)
						.toString(), (new StringBuilder()).append(endYear)
						.append(endMonth).toString());
		if (closedYearMonthlyList == null || closedYearMonthlyList.size() == 0) {
			addErrorInfo("\u4E0D\u5B58\u5728\u5DF2\u7ECF\u5C01\u5E10\u7684\u8003\u52E4\u6708\u4EFD\uFF01");
			return "error";
		}
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Attendmonthly.class);
		addCriteria(dc);
		if ("export".equals(searchOrExport)) {
			attendmonthlyList = attendmothlyBO.searchAttendmonthly(null, dc);
			setSearchOrExport("");
			return downloadToExcel(attendmonthlyList, "OExamMonthly",
					outmatchModelId, "attmEmpId");
		} else {
			attendmonthlyList = attendmothlyBO.searchAttendmonthly(page, dc);
			return "success";
		}
	}

	private void initDrillDownList() {
		years = getDrillDown("Attendperiod", new String[0]);
		IEmpTypeBO emptypebo = (IEmpTypeBO) SpringBeanFactory
				.getBean("emptypeBO");
		empType = emptypebo.FindEnabledEmpType();
		if (searchMode == null)
			searchMode = "0";
		lrTypeList = getDrillDown("LeaveType", new String[0]);
		otTypeList = getDrillDown("overtimetype", new String[0]);
	}

	private void addCriteria(DetachedCriteria dc) {
		dc.createAlias(Attendmonthly.PROP_ATTM_EMP_ID, "emp", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), "empOrgDept", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_PB_NO).toString(), "empOrgPb", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_LOCATION_NO).toString(), "empOrgLoc", 1);
		dc.add(Restrictions.eq("emp.id", attdEmpId));
		dc.add(Restrictions.in(Attendmonthly.PROP_ATTM_YEARMONTH,
				closedYearMonthlyList));
		if ("export".equals(searchOrExport)) {
			dc.setFetchMode(Attendmonthly.PROP_ATTM_DEPT, FetchMode.JOIN);
			dc.setFetchMode(Attendmonthly.PROP_ATTM_LOCATION, FetchMode.JOIN);
			dc.setFetchMode(Attendmonthly.PROP_ATTM_EMPTYPE, FetchMode.JOIN);
			dc.setFetchMode(Attendmonthly.PROP_ATTM_PB_NO, FetchMode.JOIN);
			addOrders(dc, null,
					new String[] { (new StringBuilder()).append(
							Attendmonthly.PROP_ATTM_YEARMONTH).append("-up")
							.toString() });
		} else {
			addOrders(dc, page,
					new String[] { (new StringBuilder()).append(
							Attendmonthly.PROP_ATTM_YEARMONTH).append("-up")
							.toString() });
			page.splitPage(dc);
		}
	}

	public String getEmpType(String id) {
		if (id == null || id == "")
			return "";
		for (Iterator iterator = empType.iterator(); iterator.hasNext();) {
			Emptype type = (Emptype) iterator.next();
			if (id.equals(type.getId()))
				return type.getEmptypeName();
		}

		return "";
	}

	public String formatBD(BigDecimal input) {
		if (input == null) {
			return null;
		} else {
			DecimalFormat df = new DecimalFormat("#0.0 ");
			return df.format(input);
		}
	}

	public String getAttdEmpId() {
		return attdEmpId;
	}

	public void setAttdEmpId(String attdEmpId) {
		this.attdEmpId = attdEmpId;
	}

	public List getYears() {
		return years;
	}

	public void setYears(List years) {
		this.years = years;
	}

	public String getStartYear() {
		return startYear;
	}

	public void setStartYear(String startYear) {
		this.startYear = startYear;
	}

	public String getStartMonth() {
		return startMonth;
	}

	public void setStartMonth(String startMonth) {
		this.startMonth = startMonth;
	}

	public String getEndYear() {
		return endYear;
	}

	public void setEndYear(String endYear) {
		this.endYear = endYear;
	}

	public String getEndMonth() {
		return endMonth;
	}

	public void setEndMonth(String endMonth) {
		this.endMonth = endMonth;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public List getAttendmonthlyList() {
		return attendmonthlyList;
	}

	public void setAttendmonthlyList(List attendmonthlyList) {
		this.attendmonthlyList = attendmonthlyList;
	}

	public String getSearchOrExport() {
		return searchOrExport;
	}

	public void setSearchOrExport(String searchOrExport) {
		this.searchOrExport = searchOrExport;
	}

	public String getOutmatchModelId() {
		return outmatchModelId;
	}

	public void setOutmatchModelId(String outmatchModelId) {
		this.outmatchModelId = outmatchModelId;
	}

	public FileInputStream getDocStream() {
		return docStream;
	}

	public void setDocStream(FileInputStream docStream) {
		this.docStream = docStream;
	}

	public String getContentDisposition() {
		return contentDisposition;
	}

	public void setContentDisposition(String contentDisposition) {
		this.contentDisposition = contentDisposition;
	}

	public String getServerFileName() {
		return serverFileName;
	}

	public void setServerFileName(String serverFileName) {
		this.serverFileName = serverFileName;
	}

	public static String getOutputIoName() {
		return "OExamMonthly";
	}

	public String getSearchMode() {
		return searchMode;
	}

	public void setSearchMode(String searchMode) {
		this.searchMode = searchMode;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public List getEmpType() {
		return empType;
	}

	public void setEmpType(List empType) {
		this.empType = empType;
	}

	public String getSingle() {
		return single;
	}

	public void setSingle(String single) {
		this.single = single;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public List getOtTypeList() {
		return otTypeList;
	}

	public void setOtTypeList(List otTypeList) {
		this.otTypeList = otTypeList;
	}

	public List getLrTypeList() {
		return lrTypeList;
	}

	public void setLrTypeList(List lrTypeList) {
		this.lrTypeList = lrTypeList;
	}

	public String getCurrYear() {
		return currYear;
	}

	public void setCurrYear(String currYear) {
		this.currYear = currYear;
	}

	private Pager page;
	private List attendmonthlyList;
	private List empType;
	private String attdEmpId;
	private String empName;
	private List years;
	private String startYear;
	private String startMonth;
	private String endYear;
	private String endMonth;
	private List closedYearMonthlyList;
	private String searchOrExport;
	private String outmatchModelId;
	public static final String outputIoName = "OExamMonthly";
	private FileInputStream docStream;
	private String contentDisposition;
	private String serverFileName;
	private IAttendmonthlyBO attendmothlyBO;
	private String searchMode;
	private String single;
	private Integer status;
	private List otTypeList;
	private List lrTypeList;
	private String currYear;
}
